//  泛型定义
//  传入什么类型的参数返回什么类型的参数
//  类，接口，方法的复用
// 泛型函数
function getDatass<T>(value:T):T{
    return value
}
console.log(getDatass<number>(12))

//  泛型类
class MinClass<T>{
    public minNum:T[] = []
    add(value:T):void{
        this.minNum.push(value)
    }
    min():T{
        var minValue = this.minNum[0]
        for(var i =0;i<this.minNum.length;i++){
            if(this.minNum[i]<minValue){
                minValue = this.minNum[i]
            }
        }
        return minValue
    }
}
var m1 = new MinClass<number>() //实例化 并指定T代表的类型
m1.add(1)
m1.add(3)
m1.add(-1)
var minValue:number = m1.min()
console.log(minValue)

//  泛型接口 方法
interface encrypt{
    <T>(key:T,value:T):T
}
//  泛型方法
var md5:encrypt = function<T>(key1:T,value1:T):T{
    return key1
}
console.log(md5<string>('ywj','最帅'))

interface encrypt2<T>{
    (value:T):T
}

var md:encrypt2<string> = function<T>(value:T):T{
    return value
}
console.log(md('123'))

//  类作为类型 类似与java的Javabean
class MysqlDb<T>{
    add(info:T):boolean{
        console.log(info)
        return true
    }
}

class Student{
    public name:string | undefined
    public password:string | undefined
    public id:string | undefined
    constructor(name:string|undefined,password:string|undefined,id:string|undefined){
        this.name = name
        this.password = password
        this.id = id
    }
}

const student = new Student('ywj','ywj19980121','16131018')
const mysqlDb = new MysqlDb<Student>()
mysqlDb.add(student)
